package algorithm.poj.p2000;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.net.URL;
import java.net.URLDecoder;

import algorithm.poj.Queue;


/**
 * 分析：
 * 
 * 实现：
 * 
 * 经验和教训：
 * 1. 调用方法k居然AC了，k2却是TLE（实际上BigInteger速度足够了，可能是因为数据问题导致的循环不能结束）。
 * 据说是“数据有问题的,有组数据是找不到K的,给的结果是向下取整的”
 * 
 * @author wong.tong@gmail.com
 *
 */
public class P2109 {

	public static void main(String[] args) throws Exception {

//		long start = System.currentTimeMillis();
		InputStream input = null;
		if (false) {
			input = System.in;
		} else {
			URL url = P2109.class.getResource("P2109.txt");
			File file = new File(URLDecoder.decode(url.getPath(), "UTF-8"));
			input = new FileInputStream(file);
		}
		
		BufferedReader stdin = new BufferedReader(new InputStreamReader(input));

		String lines = "";
		String line = stdin.readLine().trim();
        while (line != null && line.trim().length() > 0) {
        	lines += line + "\r\n";
        	line = stdin.readLine();
        }
        
        String output = "";
        String[] cases = lines.split("\r\n");
        for (int i = 0; i < cases.length; i ++) {
        	String c = cases[i].trim();
        	if (c.length() > 0) {
        		String[] tmp = c.split("\\s+");
        		int n = Integer.valueOf(tmp[0]);
        		BigInteger p = new BigInteger(tmp[1]);
        		output += k(n, p) + "\r\n";
        	}
        }
        
        System.out.println(output);
//        System.out.println((System.currentTimeMillis()-start) + " ms");
	}

	private static BigInteger k(int n, BigInteger p) {
		
		long a = (long)Math.pow(p.doubleValue(), 1.0f/n);
		return new BigInteger(String.valueOf(a));
	}
	
	@SuppressWarnings("unused")
	private static BigInteger k2(int n, BigInteger p) {
		
		if (n == 1) {
			return p;
		}
		String[] array = new String[1024];
		Queue<String> queue = new Queue<String>(array);
		
		BigInteger[] bits = new BigInteger[10];
		for (int i = 0; i < 10; i ++) {
			bits[i] = new BigInteger(String.valueOf(i));
			queue.put(String.valueOf(i));
		}
		
		int m = p.toString().length()/n + 1;
		
		String k = null;
		while (!queue.isEmpty()) {
			int t = 0;
			k = queue.get();
			if (new BigInteger(k).pow(n).equals(p)) {
				return new BigInteger(k);
			} else {
				t = k.length();
				if (t > m) {
					continue;
				}
			}
			BigInteger pow_s = BigInteger.TEN.pow(t+1);
			for (int i = 0; i < 10; i ++) {
				String a = bits[i] + k;
				BigInteger x = new BigInteger(a).pow(n);
				if (x.compareTo(p) != 1 && p.subtract(x).mod(pow_s).equals(BigInteger.ZERO)) {
					queue.put(a);
				}
			}
		}
		return new BigInteger("-1");
	}
}
